package org.jeecg.modules.demo.ipds.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo.ipds.entity.BitList;
import org.jeecg.modules.demo.ipds.entity.ByteFrameZu;
import org.jeecg.modules.demo.ipds.entity.TreeByteList;
import org.jeecg.modules.demo.ipds.entity.TreeByteList2;
import org.jeecg.modules.demo.ipds.entity.dto.ByteBitDto;
import org.jeecg.modules.demo.ipds.entity.dto.CalculatedValueDto;
import org.jeecg.modules.demo.ipds.entity.dto.ExportFileDto;
import org.jeecg.modules.demo.ipds.service.IBitListService;
import org.jeecg.modules.demo.ipds.service.IByteFrameZuService;
import org.jeecg.modules.demo.ipds.service.ITreeByteListService;
import org.jeecg.modules.demo.ipds.service.ITreeByteListService2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

 /**
 * @Description: 字节列表
 * @Author: jeecg-boot
 * @Date:   2024-01-31
 * @Version: V1.0
 */
@Api(tags="字节列表")
@RestController
@RequestMapping("/ipds/treeByteList")
@Slf4j
public class TreeByteListController extends JeecgController<TreeByteList, ITreeByteListService> {
	@Autowired
	private ITreeByteListService treeByteListService;

	 @Autowired
	 private IBitListService bitListService;


	 @Autowired
	 private ITreeByteListService2 treeByteListService2;

	 @Autowired
	 private IByteFrameZuService byteFrameZuService;
	 /**
	  * 分页列表查询
	  *
	  * @param treeByteList
	  * @param
	  * @return
	  */
	 //@AutoLog(value = "字节列表-分页列表查询")
	 @ApiOperation(value="字节列表-更具树协议ID查询", notes="字节列表-更具树协议ID查询")
	 @GetMapping(value = "/listByTreeId")
	 public Result<?> queryPageList(TreeByteList treeByteList) {
		 if (treeByteList.getIpTreeId()==null){
			 return Result.OK();
		 }
		 List<TreeByteList> list =	 treeByteListService.listByTreeId(treeByteList);
		 //list根据indexSx用流排序
		 list.sort((a,b)->a.getIndexSx()-b.getIndexSx());
		 return Result.OK(list);
	 }




	
	/**
	 * 分页列表查询
	 *
	 * @param treeByteList
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "字节列表-分页列表查询")
	@ApiOperation(value="字节列表-分页列表查询", notes="字节列表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<TreeByteList>> queryPageList(TreeByteList treeByteList,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<TreeByteList> queryWrapper = QueryGenerator.initQueryWrapper(treeByteList, req.getParameterMap());
		Page<TreeByteList> page = new Page<TreeByteList>(pageNo, pageSize);
		IPage<TreeByteList> pageList = treeByteListService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	
	/**
	 *   添加
	 *
	 * @param treeByteList
	 * @return
	 */
	@AutoLog(value = "字节列表-添加")
	@ApiOperation(value="字节列表-添加", notes="字节列表-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody TreeByteList treeByteList) {
		treeByteListService.save(treeByteList);
		return Result.OK("添加成功！",treeByteList);
	}



	 /**
	  *   子帧编辑
	  *
	  * @param treeByteList
	  * @return
	  */
	 @AutoLog(value = "字节列表-子帧修改")
	 @ApiOperation(value="字节列表-子帧修改", notes="字节列表-子帧修改")
	 @PutMapping(value = "/updateChildren")
	 public Result<?> updateChildren(@RequestBody TreeByteList2 treeByteList) {

		 treeByteListService2.updateById(treeByteList);
		 return Result.OK("修改成功！",treeByteList);
	 }

	 /**
	  *   添加
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "字节列表-子帧删除")
	 @ApiOperation(value="字节列表-子帧删除", notes="字节列表-子帧删除")
	 @DeleteMapping(value = "/deleteChildren")
	 public Result<?> deleteChildren(@RequestParam(name="id",required=true) String id) {

		 treeByteListService2.removeById(id);
		 return Result.OK("删除成功!");
	 }
	 @AutoLog(value = "字节列表-计算字节值")
	 @ApiOperation(value="字节列表-计算字节值", notes="字节列表-计算字节值")
	 @PutMapping(value = "/calculatedValue")
	 public Result<?> calculatedValue(@RequestBody CalculatedValueDto jsonObject){

		boolean b =  treeByteListService.calculatedValue(jsonObject);
		if (b){
		return 	Result.OK("计算成功");
		}
		 return Result.error("计算失败");
	 }










	 /**
	  *   添加
	  *
	  * @param byteBitDto
	  * @return
	  */
	 @AutoLog(value = "协议保存-批量保存")
	 @ApiOperation(value="协议保存-批量保存", notes="协议保存-批量保存")
	 @PostMapping(value = "/saveBatch")
	 public Result<String> saveBatch(@RequestBody ByteBitDto byteBitDto) {
		 if (byteBitDto.getIpTreeId()==null){
			 return Result.error("请选择协议");
		 }

		 treeByteListService.insterBatch(byteBitDto);



		 return Result.OK("保存成功！");
	 }

	 public static String getSn(Integer newSn,String length) {
		 StringBuffer snString =new StringBuffer();
		 for (int i = 0; i < Integer.parseInt(length); i++) {
			 snString.append(newSn+",");
			 newSn++;
		 }
		 int lastIndex = snString.lastIndexOf(",");
		 String  substring= snString.substring(0, lastIndex);
		 return substring;
	 }


	 public static String getLastCommaSeparatedString(String input) {
		 if (input == null || input.length() == 0) {
			 return "";
		 }

		 int lastCommaIndex = input.lastIndexOf(",");
		 if (lastCommaIndex == -1) {
			 return "";
		 }

		 return input.substring(lastCommaIndex + 1);
	 }


	
	/**
	 *  编辑
	 *
	 * @param treeByteList
	 * @return
	 */
	@AutoLog(value = "字节列表-编辑")
	@ApiOperation(value="字节列表-编辑", notes="字节列表-编辑")
	
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody TreeByteList treeByteList) {
		treeByteListService.updateById(treeByteList);

		return Result.OK("编辑成功!");
	}


	 /**
	  *  编辑
	  *
	  * @param treeByteList
	  * @return
	  */
	 @AutoLog(value = "字节列表-批量编辑")
	 @ApiOperation(value="字节列表-批量编辑", notes="字节列表-批量编辑")
	 @RequestMapping(value = "/editBatch", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> edit(@RequestBody List<TreeByteList> treeByteList) {

		 treeByteListService.updateBatchById(treeByteList);
		 for (TreeByteList byteList : treeByteList) {
			 List<BitList> bitLists = byteList.getBitLists();
			 bitListService.updateBatchById(bitLists);
		 }
		 return Result.OK("编辑成功!");
	 }

	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "字节列表-通过id删除")
	@ApiOperation(value="字节列表-通过id删除", notes="字节列表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		treeByteListService.removeById(id);

		QueryWrapper<ByteFrameZu> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("byte_id", id);
		List<ByteFrameZu> byteFrameZus = byteFrameZuService.list(queryWrapper);
		for (ByteFrameZu frameZus : byteFrameZus) {
			QueryWrapper<TreeByteList2> treeByteList2QueryWrapper = new QueryWrapper<>();
			treeByteList2QueryWrapper.eq("pid",frameZus.getId());
			List<TreeByteList2> list = treeByteListService2.list(treeByteList2QueryWrapper);

			for (TreeByteList2 treeByteList2 : list) {
				QueryWrapper<BitList> bitListQueryWrapper = new QueryWrapper<>();
				bitListQueryWrapper.eq("byte_id",treeByteList2);
				bitListService.remove(bitListQueryWrapper);
			}
			treeByteListService2.remove(treeByteList2QueryWrapper);

		}
		byteFrameZuService.remove(queryWrapper);
		QueryWrapper<BitList> bitListQueryWrapper2 = new QueryWrapper<>();
		bitListQueryWrapper2.eq("byte_id",id);
		bitListService.remove(bitListQueryWrapper2);


		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "字节列表-批量删除")
	@ApiOperation(value="字节列表-批量删除", notes="字节列表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.treeByteListService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "字节列表-通过id查询")
	@ApiOperation(value="字节列表-通过id查询", notes="字节列表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<TreeByteList> queryById(@RequestParam(name="id",required=true) String id) {
		TreeByteList treeByteList = treeByteListService.getById(id);
		if(treeByteList==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(treeByteList);
	}

	 /**
	  * 通过id查询
	  *
	  * @param json
	  * @return
	  */
	 //@AutoLog(value = "字节列表-通过id查询")
	 @ApiOperation(value="复制新增", notes="复制新增")
	 @PostMapping (value = "/copyAdd")
	 public Result<TreeByteList> copyAdd(@RequestBody JSONObject json) {
		 String id = json.get("id").toString();

		 String type = json.get("type").toString();

		 int index = Integer.parseInt(json.get("index").toString());

		 boolean b=	treeByteListService.copyAdd(id,type,index);
	return Result.OK("复制新增成功!");
	 }



    /**
    * 导出excel
    *
    * @param request
    * @param treeByteList
    */
    
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TreeByteList treeByteList) {
        return super.exportXls(request, treeByteList, TreeByteList.class, "字节列表");
    }


	 @AutoLog(value = "字节列表-导出文件二进制or十六进制文件")
	 @ApiOperation(value="字节列表-导出文件二进制or十六进制文件", notes="字节列表-导出文件二进制or十六进制文件")
	 @PostMapping (value = "/exportFile")
	 public Result<?> exportFile(@RequestBody ExportFileDto exportFileDto){

	String string	= treeByteListService.exportFile(exportFileDto);
		return Result.OK("导出成功",string);
	 }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TreeByteList.class);
    }



}
